package org.sidora.strata.context.types;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.lang.reflect.Field;
import javax.swing.JPopupMenu;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.sidora.core.context.TechnicalContext;
import org.sidora.strata.SidoraTools;
import org.sidora.strata.Storage;

/**
 * Top component which displays something.
 */
final class TechnicalTopComponent extends TopComponent {
    
    private static TechnicalTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    
    private static final String PREFERRED_ID = "TechnicalTopComponent";
    
    private TechnicalTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(TechnicalTopComponent.class, "CTL_TechnicalTopComponent"));
        setToolTipText(NbBundle.getMessage(TechnicalTopComponent.class, "HINT_TechnicalTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jTextFieldTask = new javax.swing.JTextField();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, "Task:");

        jTextFieldTask.setEditable(false);
        jTextFieldTask.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldTaskMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextFieldTask, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextFieldTask, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel6))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldTaskMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldTaskMouseClicked
        try {
            Field fiel = TechnicalContext.class.getDeclaredField("task");
            jPopupMenu = SidoraTools.generatePopupMenuCategory(Storage.getContext(), fiel, jTextFieldTask);
            jPopupMenu.show(jTextFieldTask,evt.getX(),evt.getY());
        } catch (SecurityException ex) {
            ex.printStackTrace();
        } catch (NoSuchFieldException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jTextFieldTaskMouseClicked
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextFieldTask;
    // End of variables declaration//GEN-END:variables
    private JPopupMenu jPopupMenu;
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized TechnicalTopComponent getDefault() {
        if (instance == null) {
            instance = new TechnicalTopComponent();
        }
        return instance;
    }
    
    /**
     * Obtain the TechnicalTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized TechnicalTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot find Technical component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof TechnicalTopComponent) {
            return (TechnicalTopComponent)win;
        }
        ErrorManager.getDefault().log(ErrorManager.WARNING, "There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }
    
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }
    
    public void componentOpened() {
        // TODO add custom code on component opening
    }
    
    public void componentClosed() {
        // TODO add custom code on component closing
    }
    
    /** replaces this in object stream */
    public Object writeReplace() {
        return new ResolvableHelper();
    }
    
    protected String preferredID() {
        return PREFERRED_ID;
    }
    
    final static class ResolvableHelper implements Serializable {
        private static final long serialVersionUID = 1L;
        public Object readResolve() {
            return TechnicalTopComponent.getDefault();
        }
    }
    
    public void fillItems(){
        emptyItems();
        TechnicalContext technicalContext = (TechnicalContext) Storage.getContext();
        
    }
    
    
    public void emptyItems(){
        
    }
}
